<?php

// Übersicht aller Designs anzeigen
function readDesigns() {
	global $config;
	include $config;
	$designDir = newsOrdner."designs";
	$i = 0;
	$screen = "";
	if (!is_dir($designDir)) return false;
	$designDirList = opendir($designDir);
	while (($file = readdir($designDirList)) !== false) {
		if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
		if (!is_dir($designDir."/".$file)) continue;
		if (!is_file($designDir."/".$file."/info.php")) continue;
		$designDirOpen = opendir($designDir."/".$file);
		$checked = "";
		if ($design == $file) $checked .= " checked=\"checked\" ";
		while (($designFile = readdir($designDirOpen)) !== false) {
			if ($designFile != "info.php") continue;
			include ($designDir."/".$file."/".$designFile);
			$screen = $designDir."/".$file."/img/screen.jpg";
			if (file_exists($screen)) $screen = "<img  width='150' height='100' src='$screen' alt='$file'/>";
			else $screen = ADMIN_BACKEND_NOPICTURE;
			break;
		}
		$designs[$i] = array('author'=>$author,'name'=>$name,'homepage'=>$homepage,'version'=>$version,'checked'=>$checked,'screenshot'=>$screen,'dir'=>$file);
		closedir($designDirOpen);
		$i++;
	}
	closedir($designDirList);
	$token = md5(uniqid(rand(), true));
	$_SESSION['token'] = $token;
	$tmp = new Template(templateUrl."admin_designs.tpl");
	$tmp->setOne('token',$token);
	$tmp->setContent("DESIGNS",$designs);
	echo $tmp->vorlage;
}

function designWahlSpeichern() {
	global $config;
	if (isset($_POST['designSave'])) {
		if( empty($_POST['token']) || $_POST['token'] != $_SESSION['token'] ) die;
		include $config;
		$design = htmlentities($_POST['dir']);
		configDateiSpeichern($smiliePack,$url,$sprache,$editor,$newsProSeite,$newsAufStartseite,$codepressBenutzen,$emailBenachrichtigung,$spamProtection,$design);		
		unset($_SESSION['token']);
	}
}


function designErstellen() {

	if (isset($_POST['weiterDesignErstellenSchritt1'])) {
			
		// Schritt 1 zwischenspeichern
		if (isset($_POST['name'])) $name = htmlspecialchars($_POST['name']); else $name = "";		
		if (isset($_POST['author'])) $author = htmlspecialchars($_POST['author']); else $author = "";	
		if (isset($_POST['homepage'])) $homepage = htmlspecialchars($_POST['homepage']); else $homepage = "";
		if (isset($_POST['version'])) $version = htmlspecialchars($_POST['version']); else $version = "";
		if (isset($_POST['ordner'])) $ordner = htmlspecialchars($_POST['ordner']); else $ordner = "";
		
		mkdir(newsOrdner."tmp/$ordner");
		infoSpeichern(newsOrdner."tmp/$ordner/info.php",$name,$author,$homepage,$version);
				
		$token = md5(uniqid(rand(), true));
		$_SESSION['token'] = $token;
	
		$tmp = new Template(templateUrl."admin_design_erstellen_schritt2.tpl");
		$tmp->setContent(array('token' => $token,'ordner' => $ordner));
		$tmp->setContent("TEMPLATES" , gebeFrontTemplates());
		echo $tmp->vorlage;
	
	} else if (isset($_POST['weiterDesignErstellenSchritt2'])) {
		
		if (empty($_POST['token']) || $_POST['token'] != $_SESSION['token']) die;
		if (isset($_POST['ordner'])) $ordner = htmlspecialchars($_POST['ordner']); else $ordner = "";
		
		// Schritt 2 zwischenspeichern
		foreach ($_POST as $key => $value) {
			if (preg_match('/front_.*/Uis', $key)) {
				if(!is_dir(newsOrdner."tmp/$ordner/templates")) mkdir(newsOrdner."tmp/$ordner/templates");				
				dateiSpeichern(newsOrdner."tmp/$ordner/templates/".$key.".tpl",$value,"w+");
			}
		}
		
		$token = md5(uniqid(rand(), true));
		$_SESSION['token'] = $token;
		
		$tmp = new Template(templateUrl."admin_design_erstellen_schritt3.tpl");
		$tmp->setContent(array('token' => $token,'ordner' => $ordner));
		echo $tmp->vorlage;
	
	} else  {
		// Nicht abgeschlossene Schritte löschen
		all_delete(newsOrdner."tmp");
		mkdir(newsOrdner."tmp");
		
		if (isset($_GET['name'])) $name = trim(htmlspecialchars($_GET['name'])); else $name = "";		
		if (isset($_GET['author'])) $author = trim(htmlspecialchars($_GET['author'])); else $author = "";	
		if (isset($_GET['homepage'])) $homepage = trim(htmlspecialchars($_GET['homepage'])); else $homepage = "";
		if (isset($_GET['version'])) $version = trim(htmlspecialchars($_GET['version'])); else $version = "";
		if (isset($_GET['ordner'])) $ordner = trim(htmlspecialchars($_GET['ordner'])); else $ordner = "";
	
		// Schritt 1 - Basisinfos
		$tmp = new Template(templateUrl."admin_design_erstellen_schritt1.tpl");
		$tmp->setContent(array("name"=>$name,"author"=>$author,"homepage"=>$homepage,"version"=>$version,"ordner"=>$ordner));
		echo $tmp->vorlage;
	}
}

function validiereDesignSpeichern() {

	if (isset($_POST['weiterDesignErstellenSchritt1'])) {

		if (isset($_POST['name'])) $name = trim(htmlspecialchars($_POST['name'])); else $name = "";		
		if (isset($_POST['author'])) $author = trim(htmlspecialchars($_POST['author'])); else $author = "";	
		if (isset($_POST['homepage'])) $homepage = trim(htmlspecialchars($_POST['homepage'])); else $homepage = "";
		if (isset($_POST['version'])) $version = trim(htmlspecialchars($_POST['version'])); else $version = "";
		if (isset($_POST['ordner'])) $ordner = trim(htmlspecialchars($_POST['ordner'])); else $ordner = "";

		// Eingaben Valiedieren
		// TODO: Prüfen ob Verzeichniss oder Name schon vergeben ist
		while (!isset($link)) {
			$link = "OK";
			if (!checkString($ordner,"max",40))    $link = "33";
			if (!checkSpaces($ordner,"max",40))    $link = "33";
			if (!checkString($version,"max",6))    $link = "32";
			if (!checkString($homepage,"max",150)) $link = "10";
			if (!checkUrl($homepage))              $link = "10";
			if (!checkString($author,"max",100))   $link = "17";
			if (!checkString($name,"max",100))     $link = "3";
		}
		if ($link != "OK") {
			header("Location: index.php?admin=designs&einstellen=neu&meldung=$link&name=$name&author=$author&order=$ordner&version=$version&homepage=$homepage");
			die (ADMIN_BACKEND_ERROR);
		}
		
	}
}

function gebeFrontTemplates($blackList="") {
	$i = 0;
	$templates = "";
	$templateDir = newsOrdner."templates";
	if (!is_dir($templateDir)) die("wtf?");		
	$templateList = opendir($templateDir);
	while (($file = readdir($templateList)) !== false) {
		if (preg_match('/front_.*/Uis', $file)) {
			if ($blackList != "" && is_array($blackList))  {
				if (in_array($file,$blackList)) continue;
			}
			$templates[$i] = array('name' => str_replace(".tpl","",$file));
			$i++;
		}
		
	}
	return $templates;
}

function designSpeichern() {

	if (!isset($_POST['designSpeichern'])) return;
	if (empty($_POST['token']) || $_POST['token'] != $_SESSION['token']) die;
	if (isset($_POST['ordner'])) $ordner = trim(htmlspecialchars($_POST['ordner'])); else $ordner = "";
	if (isset($_POST['content'])) $content = trim(stripcslashes($_POST['content'])); else $content = "";
	
	rename(newsOrdner."tmp/$ordner",newsOrdner."designs/$ordner");
	if(!is_dir(newsOrdner."designs/$ordner/css")) mkdir(newsOrdner."designs/$ordner/css");
	dateiSpeichern(newsOrdner."designs/$ordner/css/main.css",$content,"w+");
}

function designLoeschen() {
	global $current;
	global $einstellen;
	if ($current != "designs") return;
	if ($einstellen != "loeschen") return;
	if (isset($_GET['design'])) $design = trim(htmlspecialchars($_GET['design'])); else $design = "";
	if (empty($design)) header("Location: index.php?admin=designs&meldung=33");
	all_delete(newsOrdner."designs/$design");	
	header("Location: index.php?admin=designs&meldung=31");
}

/**
*
* CSS bearbeiten und Speichern
*
*/

function cssBearbeiten() {
	if (isset($_GET['design'])) $design = htmlspecialchars($_GET['design']); else $design = "";
	$cssFile = newsOrdner.'designs/'.$design.'/css/main.css';
	if (!is_file($cssFile)) {
		return false;
	}
	$cssDateiInhalt = "";
	$token = md5(uniqid(rand(), true));
	$_SESSION['token'] = $token;
	$newsCSS = dateiLesen($cssFile);
	foreach ($newsCSS as $line) $cssDateiInhalt .= $line;
	$tmp = new Template(templateUrl.'admin_datei_bearbeiten.tpl');
	$tmp->setContent(array('what'=>'css','who'=>'designs','fileContent'=>$cssDateiInhalt,'file'=>$design,'token'=>$token,'cancelTarget'=>"designs"));
	echo $tmp->vorlage;
	
	
}

function cssSpeichern() {
	
	if (!isset($_POST['cssSpeichern']) || !isset($_POST['content']) || !isset($_POST['file'])) return false;
	if (empty($_POST['token']) || $_POST['token'] != $_SESSION['token']) die;
		
	$cssInhalt = stripcslashes($_POST['content']);
	$cssNameAuFormular = str_replace(".css","",$_POST['file']);
	
	dateiSpeichern(newsOrdner.'designs/'.$cssNameAuFormular.'/css/main.css',$cssInhalt,"w+");
	unset($_SESSION['token']);
}

/**
*
* Infos bearbeiten und Speichern
*
*/


function infosBearbeiten() {

	if (isset($_GET['design'])) $design = htmlspecialchars($_GET['design']); else $design = "";
	if (empty($design)) return false;
	$token = md5(uniqid(rand(), true));
	$_SESSION['token'] = $token;
	
	include newsOrdner.'designs/'.$design.'/info.php';
	
	$tmp = new Template(templateUrl.'admin_design_info_bearbeiten.tpl');
	$tmp->setContent(array('design'=>$design,'author'=>$author,'homepage'=>$homepage,'name'=>$name,'version'=>$version,'token'=>$token));
	echo $tmp->vorlage;	
	
}

function infoBearbeitenSpeichern() {

	if (!isset($_POST['designInfoSpeichern'])) return false;
	if (empty($_POST['token']) || $_POST['token'] != $_SESSION['token']) die;

	if (isset($_POST['design'])) $design = htmlspecialchars($_POST['design']); else $design = "";
	if (isset($_POST['name'])) $name = htmlspecialchars($_POST['name']); else $name = "";
	if (isset($_POST['author'])) $author = htmlspecialchars($_POST['author']); else $author = "";
	if (isset($_POST['homepage'])) $homepage = htmlspecialchars($_POST['homepage']); else $homepage = "";
	if (isset($_POST['version'])) $version = htmlspecialchars($_POST['version']); else $version = "";

	//TODO: Validierung
	
	infoSpeichern(newsOrdner."designs/$design/info.php",$name,$author,$homepage,$version);
	
	unset($_SESSION['token']);	
}

function infoSpeichern($wo,$name,$author,$homepage,$version) {
	
	$buffer = "";
	$buffer .= "<?php\n\n";
	$buffer .= "\$author = \"$author\";\n\n";
	$buffer .= "\$homepage = \"$homepage\";\n\n";
	$buffer .= "\$name = \"$name\";\n\n";
	$buffer .= "\$version = \"$version\";\n\n";
	$buffer .= "?>";
	
	dateiSpeichern($wo,$buffer,"w+");
		
}

/**
* Template bearbeiten 
*
*/


function templateBearbeiten() {
	
	if (isset($_GET['design'])) $design = htmlspecialchars($_GET['design']); else $design = "";
	if (empty($design)) return false;
	
	$token = md5(uniqid(rand(), true));
	$_SESSION['token'] = $token;
	$i = 0;
	$templates = "";
	$blackList = "";
	$templateDir = newsOrdner.'designs/'.$design.'/templates';
	if (!is_dir($templateDir)) return false;
	$templateDirList = opendir($templateDir);
	while (($file = readdir($templateDirList)) !== false) {
		if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
		if (is_dir($file)) continue;
		if (!strpos($file, '.tpl', strlen($file) - 5)) continue; // 
		$templates[$i] = array("oname"=>str_replace(".tpl","",$file),"oinhalt"=>implode("",dateiLesen($templateDir."/".$file)));
		$blackList[] = $file;
		$i++;
	}
	
	$tmp = new Template(templateUrl."admin_design_template_bearbeiten.tpl");
	$tmp->setContent(array('token'=>$token,'design'=>$design));
	$tmp->setContent("TEMPLATES" , gebeFrontTemplates($blackList));
	$tmp->setContent('TEMPLATES_OFFEN',$templates);
	echo $tmp->vorlage;	
	
}

// TODO: TemplateBearbeitenSpeichern()
/*
function templateBearbeiten() {
	
	if (!isset($_POST['designTemplateAuswaehlen'])) return false;
	if (empty($_POST['token']) || $_POST['token'] != $_SESSION['token']) die;

	if (isset($_POST['design'])) $design = htmlspecialchars($_POST['design']); else $design = "";
	if (isset($_POST['templates'])) $templates = htmlspecialchars($_POST['templates']); else $templates = "";
	
	$templateDateiInhalt = "";
	$token = md5(uniqid(rand(), true));
	$_SESSION['token'] = $token;
	$templateDatei = dateiLesen(newsOrdner."designs/$design/templates/$templates");
	foreach ($templateDatei as $line) $templateDateiInhalt .= $line;
	$tmp = new Template(templateUrl.'admin_datei_bearbeiten.tpl');
	$tmp->setContent(array('what'=>'template','who'=>'designs','fileContent'=>$templateDateiInhalt,'file'=>$design,'token'=>$token,'cancelTarget'=>"designs&einstellen=template&design=$design"));
	echo $tmp->vorlage;
	
}
*/

?>